package Test01;

import java.util.ArrayList;
import java.util.Collections;

public class Books {
    ArrayList<Book> books = new ArrayList<>();
    public void addBook(Book book) {
        books.add(book);
    }

    public void showBooks() {
        for (Book book : books) {
            System.out.println(book);
        }
    }

    public void sortBooks() {
        //    按书名排序
        Collections.sort(books, (o1, o2) -> o1.getName().compareTo(o2.getName()));
    }

    public double getAveragePrice() {
        //    计算平均价格,保留两位小数,Math类
        double sum = 0;
        for (Book book : books) {
            sum += book.getPrice();
        }
        return Math.round(sum / books.size() * 100) / 100.0;
    }
}
